import requests
from requests.cookies import RequestsCookieJar
from loguru import logger
import os
import json
import sys
from work.SE_login import se_login

def retry_login(user):
    file_path = "./user/user_pool.json"
    with open(file_path, 'r') as file:
        json_data = json.load(file)
    target_account = user['account']
    matching_account = None
    for account_info in json_data:
        if account_info["account"] == target_account:
            matching_account = account_info
            break
    if matching_account:
        logger.info(f"账号: {target_account} ----> cookies 已失效，正在重新登录获取新的 cookies")
        new_user = se_login(user)
        matching_account.update(new_user)
        # 更新 json_data 中的相应账户信息
        for i, account_info in enumerate(json_data):
            if account_info["account"] == target_account:
                json_data[i] = matching_account
                break
        with open(file_path, 'w') as file:
            json.dump(json_data, file, indent=4)
            logger.success(f"账号: {user['account']} ----> 账号信息更新成功")
            return 1
    else:
        logger.info(f"cookies池不存在 {user['account']} 相关账号信息，请重新确认")
        return 0
